package org.github.core.spring.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.OutputStream;
import lombok.SneakyThrows;
import static cn.hutool.extra.spring.SpringUtil.getBean;

public interface JacksonUtil {
    @SneakyThrows
    static String json(Object obj) {
        if (obj instanceof CharSequence) {
            return obj.toString();
        }
        return getInstance().writeValueAsString(obj);
    }

    @SneakyThrows
    static void writeValue(Object obj, OutputStream output) {
        getInstance().writeValue(output, obj);
    }

    static ObjectMapper getInstance() {
        return SingletonInstance.instance;
    }

    class SingletonInstance {
        private static final ObjectMapper instance = getBean(ObjectMapper.class);
    }
}
